package com.customization.shunsheng;

import com.api.browser.bean.SearchConditionItem;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.kq.cmd.leaverules.GetLeaveRulesFormCmd;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;

import static com.api.browser.util.ConditionType.CHECKBOX;
import static com.api.browser.util.ConditionType.INPUTNUMBER;

@CommandDynamicProxy(target = GetLeaveRulesFormCmd.class )
public class GetLeaveRulesFormCmdIntercept extends AbstractCommandProxy<Map<String,Object>> {
    BaseBean bb = new BaseBean();
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        bb.writeLog("--getleaverulesformcmdinter start");
        GetLeaveRulesFormCmd cmd = (GetLeaveRulesFormCmd) command;
        Map<String,Object> params=cmd.getParams();
        Map<String,Object> result=nextExecute(cmd);

        bb.writeLog("result: " + result );
        RecordSet recordSet=new RecordSet();
        User user = cmd.getUser();
        String viewtype="";
        String ruleId = Util.null2String(params.get("ruleId"));
        bb.writeLog("ruleId: " + ruleId );

        if (StringUtil.isNotNull(ruleId)){
            viewtype="edit";
        }else {
            viewtype="add";
        }

        bb.writeLog("viewtype: " + viewtype );
        List<Object> condition1 = (List<Object>) result.get("condition");
        Map<String,Object> items = (Map<String, Object>) condition1.get(1);
        List<SearchConditionItem> item = (List<SearchConditionItem>) items.get("items");

        if ("edit".equals(viewtype)) {
            //增加一个是否入职日期折算的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"entryDiscount"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("是否入职日期折算");
            //获取是否入职日期折算的value
            Integer isEntryDis = -1;
            String getForSql = "select isEntryDis from kq_leaverulesdetail where id = ?";
            recordSet.executeQuery(getForSql,ruleId);
            while (recordSet.next()) {
                isEntryDis = Util.getIntValue(Util.null2String(recordSet.getString("isEntryDis")));
            }
            if (isEntryDis == -1 || isEntryDis == 0) {
                scConItem.setValue("0");
            } else {
                scConItem.setValue("1");
            }
            scConItem.setHelpfulTip("勾选后，该假期在员工入职当年的发放时长上进行折算，例如当年7月1日入职，发放时长则为正常时长的一半");
            item.add(scConItem);
        } else {
            //增加一个是否入职日期折算的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"entryDiscount"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("是否入职日期折算");
            scConItem.setValue("0");
            scConItem.setHelpfulTip("勾选后，该假期在员工入职当年的发放时长上进行折算，例如当年7月1日入职，发放时长则为正常时长的一半");
            item.add(scConItem);
        }

        //增加一个释放额度值保留小数位按钮INPUTNUMBER
        if ("edit".equals(viewtype)) {
            //增加一个释放额度值保留小数位按钮INPUTNUMBER
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(INPUTNUMBER);
            String[] domkeyArr = {"scaleDecimalDigit"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("释放额度值保留小数位数");
            //获取是否入职日期折算的value
            Integer scaleDecimalDigit = -1;
            String acqDecimalDigitSql = "select scaleDecimalDigit from kq_leaverulesdetail where id = ? ";
            recordSet.executeQuery( acqDecimalDigitSql,ruleId);
            while ( recordSet.next()) {
                scaleDecimalDigit = Util.getIntValue(Util.null2String(recordSet.getString("scaleDecimalDigit")));
            }

            if (scaleDecimalDigit == 0) {
                scConItem.setValue(0);//保留整数
            } else if(scaleDecimalDigit < 0) {
                scConItem.setValue(2);
            }  else {
                scConItem.setValue(scaleDecimalDigit);
            }
            scConItem.setHelpfulTip("不输入，则默认保留两位小数；输入值为0，表示保留整数；输入值为1，表示保留一位小数");
            item.add(scConItem);
        } else {
            //增加一个释放额度值保留小数位按钮INPUTNUMBER
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(INPUTNUMBER);
            String[] domkeyArr = {"scaleDecimalDigit"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("释放额度值保留小数位数");
            scConItem.setValue(2);
            scConItem.setHelpfulTip("不输入，则默认保留两位小数；输入值为0，表示保留整数；输入值为1，表示保留一位小数");
            item.add(scConItem);
        }
        //转结
        if ("edit".equals(viewtype)) {
            //增加一个是否转结的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isTransfer"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("余额是否转结至下一个有效期");
            //获取是否转结的value
            Integer isTransfer = -1;
            String getTransferSql = "select isTransfer from kq_leaverulesdetail where id = ?";
            recordSet.executeQuery(getTransferSql,ruleId);
            while (recordSet.next()) {
                isTransfer = Util.getIntValue(Util.null2String(recordSet.getString("isTransfer")));
            }
            if (isTransfer == -1 || isTransfer == 0) {
                scConItem.setValue("0");
            } else {
                scConItem.setValue("1");
            }
            scConItem.setHelpfulTip("勾选后，假期在失效时会将剩余假期值（不大于9天）转结至下一条假期余额中");
            item.add(scConItem);
        } else {
            //增加一个是否转结的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isTransfer"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("余额是否转结至下一个有效期");
            scConItem.setValue("0");
            scConItem.setHelpfulTip("勾选后，假期在失效时会将剩余假期值（不大于9天）转结至下一条假期余额中");
            item.add(scConItem);
        }

        //增加一个转结上限按钮INPUTNUMBER
        if ("edit".equals(viewtype)) {
            //增加一个释放额度值保留小数位按钮INPUTNUMBER
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(INPUTNUMBER);
            String[] domkeyArr = {"transferNumber"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("转结上限");
            //获取是否入职日期折算的value
            Integer transferNumber = -1;
            String acqTransferNumberSql = "select transferNumber from kq_leaverulesdetail where id = ? ";
            recordSet.executeQuery( acqTransferNumberSql,ruleId);
            while ( recordSet.next()) {
                transferNumber = Util.getIntValue(Util.null2String(recordSet.getString("transferNumber")));
            }

            if (transferNumber <= 0) {
                scConItem.setValue(0);//保留整数
            }  else {
                scConItem.setValue(transferNumber);
            }
//            scConItem.setHelpfulTip("不输入，则默认为0；输入值为0，表示保留整数；输入值为1，表示保留一位小数");
            item.add(scConItem);
        } else {
            //增加一个释放额度值保留小数位按钮INPUTNUMBER
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(INPUTNUMBER);
            String[] domkeyArr = {"transferNumber"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("转结上限");
            scConItem.setValue(0);
//            scConItem.setHelpfulTip("不输入，则默认保留两位小数；输入值为0，表示保留整数；输入值为1，表示保留一位小数");
            item.add(scConItem);
        }

        //增加一个余额发放方式--按孩子出生日期发放（六年内）
        if ("edit".equals(viewtype)) {
            //增加一个是否6年的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isSixYear"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("发放年限6年");
            //获取是否转结的value
            Integer isSixYear = -1;
            String getSixYearSql = "select isSixYear from kq_leaverulesdetail where id = ?";
            recordSet.executeQuery(getSixYearSql,ruleId);
            while (recordSet.next()) {
                isSixYear = Util.getIntValue(Util.null2String(recordSet.getString("isSixYear")));
            }
            if (isSixYear == -1 || isSixYear == 0) {
                scConItem.setValue("0");
            } else {
                scConItem.setValue("1");
            }
            scConItem.setHelpfulTip("勾选后，假期会根据孩子出生日期六年内都发放，需要配合余额发放方式中的按孩子出生日期发放勾选");
            item.add(scConItem);
        } else {
            //增加一个是否6年的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isSixYear"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("发放年限6年");
            scConItem.setValue("0");
            scConItem.setHelpfulTip("勾选后，假期会根据孩子出生日期六年内都发放，需要配合余额发放方式中的按孩子出生日期发放勾选");
            item.add(scConItem);

        }

        //增加一个是否不满8小时不释放check框
        if ("edit".equals(viewtype)) {
            //增加一个是否不满8小时不释放check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isDayCon"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("不满8小时额度释放为0");
            //获取是否转结的value
            Integer isDayCon = -1;
            String getDayConSql = "select isDayCon from kq_leaverulesdetail where id = ?";
            recordSet.executeQuery(getDayConSql,ruleId);
            while (recordSet.next()) {
                isDayCon = Util.getIntValue(Util.null2String(recordSet.getString("isDayCon")));
            }
            if (isDayCon == -1 || isDayCon == 0) {
                scConItem.setValue("0");
            } else {
                scConItem.setValue("1");
            }
            scConItem.setHelpfulTip("勾选后，假期基数和释放值每不满8小时，则该8小时内计为0");
            item.add(scConItem);
        } else {
            //增加一个是否6年的check框
            SearchConditionItem scConItem = new SearchConditionItem();
            scConItem.setConditionType(CHECKBOX);
            String[] domkeyArr = {"isDayCon"};
            scConItem.setDomkey(domkeyArr);
            scConItem.setLabel("不满8小时额度释放为0");
            scConItem.setValue("0");
            scConItem.setHelpfulTip("勾选后，假期基数和释放值每不满8小时，则该8小时内计为0");
            item.add(scConItem);

        }

        bb.writeLog("result: " + result );
        return result;

    }
}
